package com.myhome.controller;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Metric;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.internal.DefaultMetric;
import com.dianping.cat.message.spi.internal.DefaultMessageTree;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("student")
public class StudentController {

    //代码自己打点Demo
    @RequestMapping("/catDemo01")
    @ResponseBody
    public String CatDemo01() {
        Transaction t = Cat.newTransaction("Midas", "TestName");
        double amount = 0;
        try {
            //记录一个事件
            Cat.logEvent("err", "MyEnventName");
            //记录一个业务指标，记录次数
            Cat.logMetricForCount("PayCount");
            //记录一个业务指标，记录支付金额
            Cat.logMetricForSum("PayAmount", amount);

            //这里写自己的业务代码
            myBUssness();

            //设置状态
            t.setStatus("fail");
        } catch (Exception e) {
            t.setStatus(Transaction.SUCCESS);
            Cat.logError(e);
            throw e;
        } finally {
            t.complete();
        }
        return "1234567";
    }

    private void myBUssness() {
        Cat.logMetric("业务名称", 1, "haha", 34);
        Cat.logError("ERROR_Log", new Exception("这个是是是的"));
    }

    @RequestMapping("/catDemo02")
    @ResponseBody
    public String CatDemo02() {
        Cat.logEvent("err", "CatDemo02Envent");

        Cat.logMetric("自定义业务001", 1, 2, 3, 4, 5, 55555);

        Cat.logMetricForSum("业务SUM", 2);

        Cat.logMetricForCount("业务COUNT", 3);

        return "123456798465798";
    }

    @RequestMapping("/catDemo03")
    @ResponseBody
    public String CatDemo03() {
        Cat.logEvent("err", "CatDemoEnvent");

        Cat.logMetric("自定义业务001", 1, 2, 3, 4, 5, 55555);

        Cat.logMetricForSum("业务SUM", 2);

        Cat.logMetricForCount("业务COUNT", 3);

        return "123456798465798";
    }

    @RequestMapping("/catDemo04")
    @ResponseBody
    public String CatDemo04() {
        throw new NullPointerException("空指针异常测试");
        /*Cat.logMetric("自定义业务001",1,2,3,4,5,55555);

        Cat.logMetricForSum("业务SUM",2);

        Cat.logMetricForCount("业务COUNT",3);

        return "123456798465798";*/
    }

    @RequestMapping("/create")
    public String Create() {
        for (int i = 0; i < 26; i++) {
            CatDemo01();
        }
        return "studentCreate";
    }

    @RequestMapping("/save")
    public String Save() {
        return "studentDetail";
    }

}
